import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/base/BaseController.dart';
import 'package:xin_nuo_pda/bean/part_entity.dart';
import 'package:xin_nuo_pda/http/api/eqp_api.dart';

import 'EquipmentRepairDetailsState.dart';

class EquipmentRepairDetailsController extends BaseController {
  final EquipmentRepairDetailsState state = EquipmentRepairDetailsState();

  @override
  void onInit() {
    super.onInit();
    state.sheetId = Get.arguments;
    // getRepairInfo();
  }

  @override
  void onReady() {
    super.onReady();
    getRepairInfo();
  }

  getRepairInfo() async {
    Map<String, dynamic> params = {'SHEETID': state.sheetId};
    var result = await EqpApi.getRepairInfo(canelToken, params: params);
    if (result.ok) {
      state.repairInfoEntity = result.data;
      update();
    }
  }

  repairHandler(String type) async{
    if(state.userController.text.isEmpty){
        Get.snackbar('提示', '请输入操作员');
        return;
    }
    if(state.contentController.text.isEmpty){
      Get.snackbar('提示', '请输入维修说明');
      return;
    }
    Map<String, dynamic> params = {
      'SHEETID': state.sheetId,
      'REPAIROWNER': state.userController.text,
      'REPAIRCOMMENT': state.contentController.text,
      'STARTREPAIRTIME': state.repairInfoEntity?.TROUBLETIME,
      'ENDREPAIRTIME': state.selectDate.year.toString() +
          "-" +
          (state.selectDate.month < 10
              ? '0' + state.selectDate.month.toString()
              : state.selectDate.month.toString()) +
          '-' +
          (state.selectDate.day < 10
              ? '0' + state.selectDate.day.toString()
              : state.selectDate.day.toString()) + ' ' + state.selectDate.hour.toString() + ":" + state.selectDate.minute.toString() + ":00",
      'EVENTID': type,
      'PARTSLIST': state.partsList
    };
    var result = await EqpApi.repairHandler(canelToken, params: params);
    if(result.ok){
        Fluttertoast.showToast(msg: '处理成功');
        Get.back();
    }
  }

  void updateSelectValue(String str) {
    state.selectValue = str;
    update([state.dropDownId]);
  }

  void updateSelectDate(DateTime dateTime) {
    state.selectDate = dateTime;
    update([state.dateChooseId]);
  }

  void deletePartsData(PartEntity bean) {
    state.partsList.remove(bean);
    update([state.partsId]);
  }

  void addPartsData(String partName, String partId) {
    PartEntity entity = PartEntity();
    entity.pARTSID = partId;
    entity.pARTSNAME = partName;
    state.partsList.add(entity);
    update([state.partsId]);
  }
}
